home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / diff_2_1.lha / diff-2.1 / io.c < prev    next >
C/C++ Source or Header  |  1992-11-23  |  20KB  |  696 lines

  1. /* File I/O for GNU DIFF.
  2.    Copyright (C) 1988, 1989, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. /* Rotate a value n bits to the left. */
  23. #define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
  24. #define ROL(v, n) ((v) << (n) | (v) >> (UINT_BIT - (n)))
  25.  
  26. /* Given a hash value and a new character, return a new hash value. */
  27. #define HASH(h, c) ((c) + ROL (h, 7))
  28.  
  29. int line_cmp ();
  30.  
  31. /* Guess remaining number of lines from number N of lines so far,
  32.    size S so far, and total size T.  */
  33. #define GUESS_LINES(n,s,t) (((t) - (s)) / ((n) < 10 ? 32 : (s) / ((n)-1)) + 5)
  34.  
  35. /* Type used for fast prefix comparison in find_identical_ends.  */
  36. typedef long word;
  37.  
  38. /* Character classes.  */
  39. const char textchar[] = {
  40.   /* ISO 8859 */
  41.   0, 0, 0, 0, 0, 0, 0, 0,
  42.   2, 2, 1, 2, 2, 2, 0, 0,
  43.   0, 0, 0, 0, 0, 0, 0, 0,
  44.   0, 0, 0, 0, 0, 0, 0, 0,
  45.   2, 1, 1, 1, 1, 1, 1, 1,
  46.   1, 1, 1, 1, 1, 1, 1, 1,
  47.   1, 1, 1, 1, 1, 1, 1, 1,
  48.   1, 1, 1, 1, 1, 1, 1, 1,
  49.   1, 1, 1, 1, 1, 1, 1, 1,
  50.   1, 1, 1, 1, 1, 1, 1, 1,
  51.   1, 1, 1, 1, 1, 1, 1, 1,
  52.   1, 1, 1, 1, 1, 1, 1, 1,
  53.   1, 1, 1, 1, 1, 1, 1, 1,
  54.   1, 1, 1, 1, 1, 1, 1, 1,
  55.   1, 1, 1, 1, 1, 1, 1, 1,
  56.   1, 1, 1, 1, 1, 1, 1, 0,
  57.   0, 0, 0, 0, 0, 0, 0, 0,
  58.   0, 0, 0, 0, 0, 0, 0, 0,
  59.   0, 0, 0, 0, 0, 0, 0, 0,
  60.   0, 0, 0, 0, 0, 0, 0, 0,
  61.   2, 1, 1, 1, 1, 1, 1, 1,
  62.   1, 1, 1, 1, 1, 1, 1, 1,
  63.   1, 1, 1, 1, 1, 1, 1, 1,
  64.   1, 1, 1, 1, 1, 1, 1, 1,
  65.   1, 1, 1, 1, 1, 1, 1, 1,
  66.   1, 1, 1, 1, 1, 1, 1, 1,
  67.   1, 1, 1, 1, 1, 1, 1, 1,
  68.   1, 1, 1, 1, 1, 1, 1, 1,
  69.   1, 1, 1, 1, 1, 1, 1, 1,
  70.   1, 1, 1, 1, 1, 1, 1, 1,
  71.   1, 1, 1, 1, 1, 1, 1, 1,
  72.   1, 1, 1, 1, 1, 1, 1, 1
  73. };
  74.  
  75. /* Lines are put into equivalence classes (of lines that match in line_cmp).
  76.    Each equivalence class is represented by one of these structures,
  77.    but only while the classes are being computed.
  78.    Afterward, each class is represented by a number.  */
  79. struct equivclass
  80. {
  81.   int next;    /* Next item in this bucket. */
  82.   unsigned hash;    /* Hash of lines in this class.  */
  83.   const char *line;    /* A line that fits this class. */
  84.   int length;        /* The length of that line.  */
  85. };
  86.  
  87. /* Hash-table: array of buckets, each being a chain of equivalence classes.  */
  88. static int *buckets;
  89.   
  90. /* Number of buckets in the hash table array. */
  91. static int nbuckets;
  92.  
  93. /* Array in which the equivalence classes are allocated.
  94.    The bucket-chains go through the elements in this array.
  95.    The number of an equivalence class is its index in this array.  */
  96. static struct equivclass *equivs;
  97.  
  98. /* Index of first free element in the array `equivs'.  */
  99. static int equivs_index;
  100.  
  101. /* Number of elements allocated in the array `equivs'.  */
  102. static int equivs_alloc;
  103.  
  104. /* Check for binary files and compare them for exact identity.  */
  105.  
  106. /* Return 1 if BUF contains a non text character.
  107.    SIZE is the number of characters in BUF.  */
  108.  
  109. #define binary_file_p(buf, size) (memchr (buf, '\0', size) != 0)
  110.  
  111. /* Get ready to read the current file.
  112.    Return nonzero if SKIP_TEST is zero,
  113.    and if it appears to be a binary file.  */
  114.  
  115. int
  116. sip (current, skip_test)
  117.      struct file_data *current;
  118.      int skip_test;
  119. {
  120.   /* If we have a nonexistent file at this stage, treat it as empty.  */
  121.   if (current->desc < 0)
  122.     {
  123.       /* Leave room for a sentinel.  */
  124.       current->buffer = xmalloc (sizeof (word));
  125.       current->bufsize = sizeof (word);
  126.       current->buffered_chars = 0;
  127.     }
  128.   else
  129.     {
  130.       current->bufsize = current->buffered_chars
  131.     = STAT_BLOCKSIZE (current->stat);
  132.  
  133.       if (S_ISREG (current->stat.st_mode))
  134.     /* Get the size out of the stat block.
  135.        Allocate enough room for appended newline and sentinel.
  136.        Allocate at least one block, to prevent overrunning the buffer
  137.        when comparing growing binary files.  */
  138.     current->bufsize = max (current->bufsize,
  139.                 current->stat.st_size + sizeof (word) + 1);
  140.  
  141.       current->buffer = xmalloc (current->bufsize);
  142.       if (skip_test)
  143.     current->buffered_chars = 0;
  144.       else
  145.     {
  146.       /* Check first part of file to see if it's a binary file.  */
  147.       current->buffered_chars = read (current->desc,
  148.                       current->buffer,
  149.                       current->buffered_chars);
  150.       if (current->buffered_chars < 0)
  151.         pfatal_with_name (current->name);
  152.       return binary_file_p (current->buffer, current->buffered_chars);
  153.     }
  154.     }
  155.   
  156.   return 0;
  157. }
  158.  
  159. /* Slurp the rest of the current file completely into memory.  */
  160.  
  161. void
  162. slurp (current)
  163.      struct file_data *current;
  164. {
  165.   int cc;
  166.  
  167.   if (current->desc < 0)
  168.     /* The file is nonexistent.  */
  169.     ;
  170.   else if (S_ISREG (current->stat.st_mode))
  171.     {
  172.       /* It's a regular file; slurp in the rest all at once.  */
  173.       cc = current->stat.st_size - current->buffered_chars;
  174.       if (cc)
  175.     {
  176.       cc = read (current->desc,
  177.              current->buffer + current->buffered_chars,
  178.              cc);
  179.       if (cc < 0)
  180.         pfatal_with_name (current->name);
  181.       current->buffered_chars += cc;
  182.     }
  183.     }
  184.   /* It's not a regular file; read it, growing the buffer as needed.  */
  185.   else if (always_text_flag || current->buffered_chars != 0)
  186.     {
  187.       for (;;)
  188.     {
  189.       if (current->buffered_chars == current->bufsize)
  190.         {
  191.           current->bufsize = current->bufsize * 2;
  192.           current->buffer = (char *) xrealloc (current->buffer,
  193.                            current->bufsize);
  194.         }
  195.       cc = read (current->desc,
  196.              current->buffer + current->buffered_chars,
  197.              current->bufsize - current->buffered_chars);
  198.       if (cc == 0)
  199.         break;
  200.       if (cc < 0)
  201.         pfatal_with_name (current->name);
  202.       current->buffered_chars += cc;
  203.     }
  204.       /* Allocate just enough room for appended newline and sentinel.  */
  205.       current->bufsize = current->buffered_chars + sizeof (word) + 1;
  206.       current->buffer = (char *) xrealloc (current->buffer, current->bufsize);
  207.     }
  208. }
  209.  
  210. /* Split the file into lines, simultaneously computing the equivalence class for
  211.    each line. */
  212.  
  213. static void
  214. find_and_hash_each_line (current)
  215.      struct file_data *current;
  216. {
  217.   unsigned h;
  218.   const unsigned char *p = (const unsigned char *) current->prefix_end;
  219.   unsigned char c;
  220.   int i, length, *bucket;
  221.  
  222.   /* Cache often-used quantities in local variables to help the compiler.  */
  223.   const char **linbuf = current->linbuf;
  224.   int alloc_lines = current->alloc_lines;
  225.   int line = 0;
  226.   int linbuf_base = current->linbuf_base;
  227.   int *cureqs = (int *) xmalloc (alloc_lines * sizeof (int));
  228.   struct equivclass *eqs = equivs;
  229.   int eqs_index = equivs_index;
  230.   int eqs_alloc = equivs_alloc;
  231.   const char *suffix_begin = current->suffix_begin;
  232.   const char *bufend = current->buffer + current->buffered_chars;
  233.   const char *incomplete_tail
  234.     = current->missing_newline && ROBUST_OUTPUT_STYLE (output_style)
  235.       ? bufend : (const char *) 0;
  236.   int varies = length_varies;
  237.  
  238.   while ((const char *) p < suffix_begin)
  239.     {
  240.       const char *ip = (const char *) p;
  241.  
  242.       /* Compute the equivalence class for this line.  */
  243.  
  244.       h = 0;
  245.  
  246.       /* Hash this line until we find a newline. */
  247.       if (ignore_case_flag)
  248.     {
  249.       if (ignore_all_space_flag)
  250.         while ((c = *p++) != '\n')
  251.           {
  252.         if (! Is_space (c))
  253.           h = HASH (h, isupper (c) ? tolower (c) : c);
  254.           }
  255.       else if (ignore_space_change_flag)
  256.         while ((c = *p++) != '\n')
  257.           {
  258.         if (c == ' ' || c == '\t')
  259.           {
  260.             while ((c = *p++) == ' ' || c == '\t')
  261.               ;
  262.             if (c == '\n')
  263.               break;
  264.             h = HASH (h, ' ');
  265.           }
  266.         /* C is now the first non-space.  */
  267.         h = HASH (h, isupper (c) ? tolower (c) : c);
  268.           }
  269.       else
  270.         while ((c = *p++) != '\n')
  271.           h = HASH (h, isupper (c) ? tolower (c) : c);
  272.     }
  273.       else
  274.     {
  275.       if (ignore_all_space_flag)
  276.         while ((c = *p++) != '\n')
  277.           {
  278.         if (! Is_space (c))
  279.           h = HASH (h, c);
  280.           }
  281.       else if (ignore_space_change_flag)
  282.         while ((c = *p++) != '\n')
  283.           {
  284.         if (c == ' ' || c == '\t')
  285.           {
  286.             while ((c = *p++) == ' ' || c == '\t')
  287.               ;
  288.             if (c == '\n')
  289.               break;
  290.             h = HASH (h, ' ');
  291.           }
  292.         /* C is now the first non-space.  */
  293.         h = HASH (h, c);
  294.           }
  295.       else
  296.         while ((c = *p++) != '\n')
  297.           h = HASH (h, c);
  298.     }
  299.  
  300.       bucket = &buckets[h % nbuckets];
  301.       length = (const char *) p - ip - ((const char *) p == incomplete_tail);
  302.       for (i = *bucket;  ;  i = eqs[i].next)
  303.     if (!i)
  304.       {
  305.         /* Create a new equivalence class in this bucket. */
  306.         i = eqs_index++;
  307.         if (i == eqs_alloc)
  308.           eqs = (struct equivclass *)
  309.               xrealloc (eqs, (eqs_alloc*=2) * sizeof(*eqs));
  310.         eqs[i].next = *bucket;
  311.         eqs[i].hash = h;
  312.         eqs[i].line = ip;
  313.         eqs[i].length = length;
  314.         *bucket = i;
  315.         break;
  316.       }
  317.     else if (eqs[i].hash == h
  318.          && (eqs[i].length == length || varies)
  319.          && ! line_cmp (eqs[i].line, eqs[i].length, ip, length))
  320.       /* Reuse existing equivalence class.  */
  321.         break;
  322.  
  323.       /* Maybe increase the size of the line table. */
  324.       if (line == alloc_lines)
  325.     {
  326.       /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */
  327.       alloc_lines = 2 * alloc_lines - linbuf_base;
  328.       cureqs = (int *) xrealloc (cureqs, alloc_lines * sizeof (*cureqs));
  329.       linbuf = (const char **) xrealloc (linbuf + linbuf_base,
  330.                          (alloc_lines - linbuf_base)
  331.                          * sizeof (*linbuf))
  332.            - linbuf_base;
  333.     }
  334.       linbuf[line] = ip;
  335.       cureqs[line] = i;
  336.       ++line;
  337.     }
  338.  
  339.   current->buffered_lines = line;
  340.  
  341.   for (i = 0;  ;  i++)
  342.     {
  343.       /* Record the line start for lines in the suffix that we care about.
  344.          Record one more line start than lines,
  345.      so that we can compute the length of any buffered line.  */
  346.       if (line == alloc_lines)
  347.     {
  348.       /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */
  349.       alloc_lines = 2 * alloc_lines - linbuf_base;
  350.       linbuf = (const char **) xrealloc (linbuf + linbuf_base,
  351.                          (alloc_lines - linbuf_base)
  352.                          * sizeof (*linbuf))
  353.            - linbuf_base;
  354.     }
  355.       linbuf[line] = (const char *) p;
  356.     
  357.       if ((const char *) p == bufend)
  358.     {
  359.       linbuf[line]  -=  (const char *) p == incomplete_tail;
  360.       break;
  361.     }
  362.  
  363.       if (context <= i && no_diff_means_no_output)
  364.     break;
  365.  
  366.       line++;
  367.  
  368.       while (*p++ != '\n')
  369.     ;
  370.     }
  371.  
  372.   /* Done with cache in local variables.  */
  373.   current->linbuf = linbuf;
  374.   current->valid_lines = line;
  375.   current->alloc_lines = alloc_lines;
  376.   current->equivs = cureqs;
  377.   equivs = eqs;
  378.   equivs_alloc = eqs_alloc;
  379.   equivs_index = eqs_index;
  380. }
  381.  
  382. /* Prepare the end of the text.  Make sure it's initialized.
  383.    Make sure text ends in a newline,
  384.    but remember that we had to add one unless -B is in effect.  */
  385.  
  386. static void
  387. prepare_text_end (current)
  388.      struct file_data *current;
  389. {
  390.   int buffered_chars = current->buffered_chars;
  391.   char *p = current->buffer;
  392.  
  393.   if (buffered_chars == 0 || p[buffered_chars - 1] == '\n')
  394.     current->missing_newline = 0;
  395.   else
  396.     {
  397.       p[buffered_chars++] = '\n';
  398.       current->buffered_chars = buffered_chars;
  399.       current->missing_newline = ! ignore_blank_lines_flag;
  400.     }
  401.   
  402.   /* Don't use uninitialized storage when planting or using sentinels.  */
  403.   if (p)
  404.     bzero (p + buffered_chars, sizeof (word));
  405. }
  406.  
  407. /* Given a vector of two file_data objects, find the identical
  408.    prefixes and suffixes of each object. */
  409.  
  410. static void
  411. find_identical_ends (filevec)
  412.      struct file_data filevec[];
  413. {
  414.   word *w0, *w1;
  415.   char *p0, *p1, *buffer0, *buffer1;
  416.   const char *end0, *beg0;
  417.   const char **linbuf0, **linbuf1;
  418.   int i, lines;
  419.   int n0, n1, alloc_lines0, alloc_lines1;
  420.   int buffered_prefix, prefix_count, prefix_mask;
  421.  
  422.   slurp (&filevec[0]);
  423.   if (filevec[0].desc != filevec[1].desc)
  424.     slurp (&filevec[1]);
  425.   else
  426.     {
  427.       filevec[1].buffer = filevec[0].buffer;
  428.       filevec[1].bufsize = filevec[0].bufsize;
  429.       filevec[1].buffered_chars = filevec[0].buffered_chars;
  430.     }
  431.   for (i = 0; i < 2; i++)
  432.     prepare_text_end (&filevec[i]);
  433.  
  434.   /* Find identical prefix.  */
  435.  
  436.   p0 = buffer0 = filevec[0].buffer;
  437.   p1 = buffer1 = filevec[1].buffer;
  438.  
  439.   n0 = filevec[0].buffered_chars;
  440.   n1 = filevec[1].buffered_chars;
  441.  
  442.   if (p0 == p1)
  443.     /* The buffers are the same; sentinels won't work.  */
  444.     p0 = p1 += n1;
  445.   else
  446.     {
  447.       /* Insert end sentinels, in this case characters that are guaranteed
  448.      to make the equality test false, and thus terminate the loop.  */
  449.  
  450.       if (n0 < n1)
  451.     p0[n0] = ~p1[n0];
  452.       else
  453.     p1[n1] = ~p0[n1];
  454.  
  455.       /* Loop until first mismatch, or to the sentinel characters.  */
  456.  
  457.       /* Compare a word at a time for speed.  */
  458.       w0 = (word *) p0;
  459.       w1 = (word *) p1;
  460.       while (*w0++ == *w1++)
  461.     ;
  462.       --w0, --w1;
  463.  
  464.       /* Do the last few bytes of comparison a byte at a time.  */
  465.       p0 = (char *) w0;
  466.       p1 = (char *) w1;
  467.       while (*p0++ == *p1++)
  468.     ;
  469.       --p0, --p1;
  470.  
  471.       /* Don't mistakenly count missing newline as part of prefix. */
  472.       if (ROBUST_OUTPUT_STYLE (output_style)
  473.       && (buffer0 + n0 - filevec[0].missing_newline < p0)
  474.          !=
  475.          (buffer1 + n1 - filevec[1].missing_newline < p1))
  476.     --p0, --p1;
  477.     }
  478.  
  479.   /* Now P0 and P1 point at the first nonmatching characters.  */
  480.  
  481.   /* Skip back to last line-beginning in the prefix.  */
  482.   while (p0 != buffer0 && p0[-1] != '\n')
  483.     --p0, --p1;
  484.  
  485.   /* Record the prefix.  */
  486.   filevec[0].prefix_end = p0;
  487.   filevec[1].prefix_end = p1;
  488.  
  489.   /* Find identical suffix.  */
  490.  
  491.   /* P0 and P1 point beyond the last chars not yet compared.  */
  492.   p0 = buffer0 + n0;
  493.   p1 = buffer1 + n1;
  494.  
  495.   if (! ROBUST_OUTPUT_STYLE (output_style)
  496.       || filevec[0].missing_newline == filevec[1].missing_newline)
  497.     {
  498.       end0 = p0;    /* Addr of last char in file 0.  */
  499.  
  500.       /* Get value of P0 at which we should stop scanning backward:
  501.      this is when either P0 or P1 points just past the last char
  502.      of the identical prefix.  */
  503.       beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1);
  504.  
  505.       /* Scan back until chars don't match or we reach that point.  */
  506.       while (p0 != beg0)
  507.     if (*--p0 != *--p1)
  508.       {
  509.         /* Point at the first char of the matching suffix.  */
  510.         ++p0, ++p1;
  511.         beg0 = p0;
  512.         break;
  513.       }
  514.  
  515.       /* Are we at a line-beginning in both files?  If not, add the rest of
  516.      this line to the main body.  Also, add one more line, even though it
  517.      is the same in both files, because shift_boundaries may need it.  */
  518.       i = !((buffer0 == p0 || p0[-1] == '\n')
  519.         &&
  520.         (buffer1 == p1 || p1[-1] == '\n'));
  521.       while (0 <= i-- && p0 != end0)
  522.     while (*p0++ != '\n')
  523.       ;
  524.  
  525.       p1 += p0 - beg0;
  526.     }
  527.  
  528.   /* Record the suffix.  */
  529.   filevec[0].suffix_begin = p0;
  530.   filevec[1].suffix_begin = p1;
  531.  
  532.   /* Calculate number of lines of prefix to save.
  533.  
  534.      prefix_count == 0 means save the whole prefix;
  535.      we need this with for options like -D that output the whole file.
  536.      We also need it for options like -F that output some preceding line;
  537.      at least we will need to find the last few lines,
  538.      but since we don't know how many, it's easiest to find them all.
  539.  
  540.      Otherwise, prefix_count != 0.  Save just prefix_count lines at start
  541.      of the line buffer; they'll be moved to the proper location later.
  542.      Handle 1 more line than the context says (because we count 1 too many),
  543.      rounded up to the next power of 2 to speed index computation.  */
  544.  
  545.   if (no_diff_means_no_output && ! function_regexp_list)
  546.     {
  547.       for (prefix_count = 1;  prefix_count < context + 1;  prefix_count *= 2)
  548.     ;
  549.       prefix_mask = prefix_count - 1;
  550.       alloc_lines0
  551.     = prefix_count
  552.       + GUESS_LINES (0, 0, p0 - filevec[0].prefix_end)
  553.       + context;
  554.     }
  555.   else
  556.     {
  557.       prefix_count = 0;
  558.       prefix_mask = ~0;
  559.       alloc_lines0 = GUESS_LINES (0, 0, n0);
  560.     }
  561.  
  562.   lines = 0;
  563.   linbuf0 = (const char **) xmalloc (alloc_lines0 * sizeof (*linbuf0));
  564.  
  565.   /* If the prefix is needed, find the prefix lines.  */
  566.   if (! (no_diff_means_no_output
  567.      && filevec[0].prefix_end == p0
  568.      && filevec[1].prefix_end == p1))
  569.     {
  570.       p0 = buffer0;
  571.       end0 = filevec[0].prefix_end;
  572.       while (p0 != end0)
  573.     {
  574.       int l = lines++ & prefix_mask;
  575.       if (l == alloc_lines0)
  576.         linbuf0 = (const char **) xrealloc (linbuf0, (alloc_lines0 *= 2)
  577.                              * sizeof(*linbuf0));
  578.       linbuf0[l] = p0;
  579.       while (*p0++ != '\n')
  580.         ;
  581.     }
  582.     }
  583.   buffered_prefix = prefix_count && context < lines ? context : lines;
  584.  
  585.   /* Allocate line buffer 1.  */
  586.   alloc_lines1
  587.     = buffered_prefix
  588.       + GUESS_LINES (lines, filevec[1].prefix_end - buffer1,
  589.              prefix_count ? filevec[1].suffix_begin - buffer1 : n1)
  590.       + context;
  591.   linbuf1 = (const char **) xmalloc (alloc_lines1 * sizeof (*linbuf1));
  592.  
  593.   if (buffered_prefix != lines)
  594.     {
  595.       /* Rotate prefix lines to proper location.  */
  596.       for (i = 0;  i < buffered_prefix;  i++)
  597.     linbuf1[i] = linbuf0[(lines - context + i) & prefix_mask];
  598.       for (i = 0;  i < buffered_prefix;  i++)
  599.     linbuf0[i] = linbuf1[i];
  600.     }
  601.  
  602.   /* Initialize line buffer 1 from line buffer 0.  */
  603.   for (i = 0; i < buffered_prefix; i++)
  604.     linbuf1[i] = linbuf0[i] - buffer0 + buffer1;
  605.  
  606.   /* Record the line buffer, adjusted so that
  607.      linbuf*[0] points at the first differing line.  */
  608.   filevec[0].linbuf = linbuf0 + buffered_prefix;
  609.   filevec[1].linbuf = linbuf1 + buffered_prefix;
  610.   filevec[0].linbuf_base = filevec[1].linbuf_base = - buffered_prefix;
  611.   filevec[0].alloc_lines = alloc_lines0 - buffered_prefix;
  612.   filevec[1].alloc_lines = alloc_lines1 - buffered_prefix;
  613.   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  614. }
  615.  
  616. /* Largest primes less than some power of two, for nbuckets.  Values range
  617.    from useful to preposterous.  If one of these numbers isn't prime
  618.    after all, don't blame it on me, blame it on primes (6) . . . */
  619. static const int primes[] =
  620. {
  621.   509,
  622.   1021,
  623.   2039,
  624.   4093,
  625.   8191,
  626.   16381,
  627.   32749,
  628.   65521,
  629.   131071,
  630.   262139,
  631.   524287,
  632.   1048573,
  633.   2097143,
  634.   4194301,
  635.   8388593,
  636.   16777213,
  637.   33554393,
  638.   67108859,            /* Preposterously large . . . */
  639.   134217689,
  640.   268435399,
  641.   536870909,
  642.   1073741789,
  643.   2147483647,
  644.   0
  645. };
  646.  
  647. /* Given a vector of two file_data objects, read the file associated
  648.    with each one, and build the table of equivalence classes.
  649.    Return 1 if either file appears to be a binary file.  */
  650.  
  651. int
  652. read_files (filevec)
  653.      struct file_data filevec[];
  654. {
  655.   int i;
  656.   int skip_test = always_text_flag | no_details_flag;
  657.   int appears_binary = no_details_flag | sip (&filevec[0], skip_test);
  658.  
  659.   if (filevec[0].desc != filevec[1].desc)
  660.     appears_binary |= sip (&filevec[1], skip_test | appears_binary);
  661.   else
  662.     {
  663.       filevec[1].buffer = filevec[0].buffer;
  664.       filevec[1].bufsize = filevec[0].bufsize;
  665.       filevec[1].buffered_chars = filevec[0].buffered_chars;
  666.     }
  667.   if (appears_binary)
  668.     return 1;
  669.  
  670.   find_identical_ends (filevec);
  671.  
  672.   equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1;
  673.   equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivclass));
  674.   /* Equivalence class 0 is permanently safe for lines that were not
  675.      hashed.  Real equivalence classes start at 1. */
  676.   equivs_index = 1;
  677.  
  678.   for (i = 0;  primes[i] < equivs_alloc / 3;  i++)
  679.     if (! primes[i])
  680.       abort ();
  681.   nbuckets = primes[i];
  682.  
  683.   buckets = (int *) xmalloc (nbuckets * sizeof (*buckets));
  684.   bzero (buckets, nbuckets * sizeof (*buckets));
  685.  
  686.   for (i = 0; i < 2; ++i)
  687.     find_and_hash_each_line (&filevec[i]);
  688.  
  689.   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
  690.  
  691.   free (equivs);
  692.   free (buckets);
  693.  
  694.   return 0;
  695. }
  696.